<template>
    <div class="details">
         <!-- 搜索部分 -->
         <div class="search_container">
            <div class="leftjt">
              <router-link to="/Index">
                <img class="zjts" :src="picture.zjt" alt="" />
              </router-link>
            </div>
            <div class="search">
                  <input v-model="input" type="text" placeholder="请输入搜索内容">
                  <img :src="picture.serach" alt="ztj">
                  <img :src="picture.jd" alt="jd">
            </div>
          </div>

          <div class="seach_img">
            <img :src="pic+goods.picture" alt="">
          </div>
           
            <!-- 价格 -->
            <div class="price_container">
                <div class="left">
                    <span>￥</span>
                    <span>{{goods.price}}</span>
                </div>
                <div class="right">
                    <span>热销款￥</span>
                    <span>{{goods.price}}</span>
                </div>
            </div>

                <!-- 优惠券单元格 -->
                    <van-coupon-cell
                    :coupons="coupons"
                    :chosen-coupon="chosenCoupon"
                    @click="showList = true"
                    />
                    <!-- 优惠券列表 -->
                    <van-popup
                    v-model="showList"
                    round
                    position="bottom"
                    style="height: 90%; padding-top: 4px;"
                    >
                    <van-coupon-list
                    :coupons="coupons"
                    :chosen-coupon="chosenCoupon"
                    :disabled-coupons="disabledCoupons"
                    @change="onChange"
                    @exchange="onExchange"
                    />
                    </van-popup>

                <div class="tianmao">
                    <div>{{goods.skyCat}}</div>
                    <div>{{goods.pay}}</div>
                    <div style="margin-right: 50px;">{{goods.comment}}</div>
                    <div>月销:{{goods.num}}</div>
                </div>
            <!-- 简介 -->
            <p> {{ goods.name }} </p>
            <van-cell title="分享" icon="share-o" @click="showShare = true"  />
            <van-share-sheet class="share-sheets" v-model="showShare" title="立即分享给好友" :options="options" @select="onSelect"/>
            <div class="selects">
                <div>选择:</div>
                <div>{{goods.select}}</div>
            </div>
            <div class="selects">
                <div>发货:</div>
                <div>
                    <div style="font-size:12px;">北京 | 快递:免运费</div>
                    <div style="font-size:12px;">现货,付款后48小时内发货</div>
                    <router-link to="/Peisong" class="Peisong">配送至:江西省 九江市 湖口县 大龙乡</router-link>
                </div>
            </div>

            <!-- 加入购物车，立即购买 -->
          <van-goods-action class="goods">
            <van-goods-action-icon icon="chat-o" text="客服" color="#ee0a24" />
            <van-goods-action-icon icon="cart-o" @click="goshopcar" text="购物车" />
            <van-goods-action-icon icon="star" text="已收藏"/>
            <van-goods-action-button @click="add" type="warning" text="加入购物车" />
            <van-goods-action-button type="danger" text="立即购买" />
          </van-goods-action>
    </div>
</template>

<script>
import pubsub from 'pubsub-js'
// 引入axios
import axios from 'axios'
import { ref } from 'vue';
export default {
    setup() {
    const coupon = {
      available: 1,
      condition: '无门槛\n最多优惠12元',
      reason: '',
      value: 150,
      name: '优惠券名称',
      startAt: 1489104000,
      endAt: 1514592000,
      valueDesc: '1.5',
      unitDesc: '元',
    };

    const coupons = ref([coupon]);
    const showList = ref(false);
    const chosenCoupon = ref(-1);

    const onChange = (index) => {
      showList.value = false;
      chosenCoupon.value = index;
    };
    const onExchange = (code) => {
      coupons.value.push(coupon);
    };

    return {
      coupons,
      showList,
      onChange,
      onExchange,
      chosenCoupon,
      disabledCoupons: [coupon],
    };
  },
    name: 'Detail',
    data() {
    return {
      goods:{},
      pic:'http://localhost:3000/',
      showShare: false,
      options: [
        { name: '微信', icon: 'wechat' },
        { name: '微博', icon: 'weibo' },
        { name: '复制链接', icon: 'link', description: 'http://www.baidu.com' },
        { name: 'QQ', icon: 'qq' },
      ],
      current: 0,

      picture:{
                jd:require('@/assets/images/京东.png'),
                serach:require('@/assets/images/搜索.png'),
                zjt: require("@/assets/images/左箭头.png"),
            }, 
            input:'',
            
    };
  },
      created(){
        console.log(this.$route.query.id);
       
        
      },

      mounted() {
// 请求接口，获取recommend里面的id
        let ajax = (type,url) => {
                    return new Promise((resolve, reject) => {
                axios({
                    method:type,
                    url:url,
                    // 前端给后台传数据
                    params:{
                    id:this.$route.query.id
                    }
                    
                })
                  .then((result) => {
                  resolve(result)
                  // console.log(result);
                }).catch((err) => {
                  reject(err)
                });
                });
                }
                let good = async () => {
                    let res = await ajax('GET','http://localhost:3000/api/details')
                    console.log(res.data);
                    this.goods = res.data
                } 
                good()
              
      },

    methods: {
      onSelect(option) {
      Toast(option.name);
      this.showShare = false;
    },
    // 发送消息
    goshopcar(){
     this.$router.push({
      path:'/shopcar'
     })
    },
    add(){
      alert("加入购物车成功")
    }
    
  },
};
</script>

<style scoped>
.details{
    width: 400px;
    margin: 0 auto;
    height: 1000px;
}
.custom-indicator {
    position: absolute;
    right: 5px;
    bottom: 5px;
    padding: 2px 5px;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.1);
  }
  .my-swipe .van-swipe-item {
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    background-color: #39a9ed;
  }
  .goods{
    width: 400px;
    margin: 0 auto;
  }
  .search_container{
    display: flex;
    height: 44px;
    position: fixed;
	top: 0px;
    z-index: 1000;
    background: linear-gradient(white, rgb(241, 241, 241));
    border-bottom: 2px solid #e0e0e0;
  }
  
  .search{
    display: flex;
    width: 359px;
  }
  .search img{
    width: 25px;
    height: 25px;
    margin-top: 10px;
    cursor: pointer;
  }
  .search input{
    width: 280px;
    margin-top: 10px;
    border: 1px solid rgb(216, 216, 216);
    outline: none;
    height: 25px;
    margin-left: 15px;
    border-radius: 18px;
  }
  .header{
    margin: 0 auto;
    width: 400px;
    height: 44px;
    display: flex;
    background: linear-gradient(white, rgb(241, 241, 241));
    border-bottom: 2px solid #e0e0e0;
  }
  .zjts{
    width: 30px;
    height: 30px;
    margin-top: 9.5px;
    margin-left: 10px;
  }
  .price_container{
    display: flex;
    margin-top: 15px;
  }
  .price_container .right{
    margin-left: 40px;
  }
  .price_container .left span:nth-of-type(1){
    color: red;
    font-size: 13px;
  }
  .price_container .left span:nth-of-type(2){
    color: red;
    font-size: 16px;
  }
  .price_container .right span:nth-of-type(1){
    color: #8b8b8b;
    font-size: 12px;
  }
  .price_container .right span:nth-of-type(2){
    color: #8b8b8b;
    font-size: 16px;
  }
  .tianmao{
    display: flex;
  }
  .tianmao>div:nth-of-type(1){
    width: 60px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    color:#fff;
    border-radius: 20px;
    font-size: 13px;
    background: red;
  }
  .tianmao>div:nth-of-type(2){
   margin-right: 30px;
   margin-left: 20px;
   line-height: 30px;
   font-size: 14px;
   color: gray;
  }
  
  .share-sheets{
    left: auto;
    width: 400px !important;
  }
  .van-popup{
    left: auto;
    width: 400px;
    padding-top: 0px;
  }
  .selects{
    display: flex;
  }
  .selects>div:nth-of-type(1){
    width: 25%;
    color: gray;
    font-size: 14px;
  }
  .Peisong{
    text-decoration: none;
    color: #333;
    font-size: 12px;
  }
  .seach_img img{
    width: 399px;
    height: 399px;
  }
  .seach_img{
    margin-top: 44px;
  }
</style>